﻿using Infortime.Domains.Entities;
using Infortime.Repositories;
using Volo.Abp.Domain.Repositories;
using Volo.Abp;
using Volo.Abp.Domain.Services;
using System.Text.RegularExpressions;
using Infortime.Consts;
using Volo.Abp.DependencyInjection;
using Infortime.Dtos;

namespace Infortime.Domains.Managers
{
    [Dependency(ServiceLifetime.Singleton)]
    public class UserManager : DomainService, IRemoteService
    {
        public IUserRepository UserRepository { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <returns></returns>
        public async Task CreateAsync(UserInputDto input)
        {
            UserAggregateRoot userEntity = new()
            {
                Name = input.Name,
                UserName = input.UserName,
                Phone = input.Phone,
                Nick = input.Nick
            };
            userEntity.EncryPassword.Password = input.Password;
            //校验用户名
            ValidateUserName(userEntity);

            if (userEntity.EncryPassword?.Password.Length < 6)
            {
                throw new UserFriendlyException(UserConst.Create_Passworld_Error);
            }

            if (userEntity.Phone is not null)
            {
                if (await UserRepository.AnyAsync(x => x.Phone == userEntity.Phone))
                {
                    throw new UserFriendlyException(UserConst.Phone_Repeat);
                }
            }
            var isExist = await UserRepository.AnyAsync(x => x.UserName == userEntity.UserName);
            if (isExist)
            {
                throw new UserFriendlyException(UserConst.Exist);
            }
            var entity = await UserRepository.InsertAsync(userEntity);
        }

        public async Task<List<UserAggregateRoot>> GetListAsync()
        {
            List<UserAggregateRoot> userAggregateRoots = await UserRepository.GetListAsync();
            return userAggregateRoots;
        }

        public async Task<string> DisableAsync(Guid id)
        {
            UserAggregateRoot target = await UserRepository.GetAsync(id);
            await UserRepository.DeleteAsync(id);
            return $"用户{target.UserName}已经禁用";
        }

        private void ValidateUserName(UserAggregateRoot input)
        {
            if (input.UserName == UserConst.Admin || input.UserName == UserConst.TenantAdmin)
            {
                throw new UserFriendlyException("用户名无效注册！");
            }

            if (input.UserName.Length < 2)
            {
                throw new UserFriendlyException("账号名需大于等于2位！");
            }

            // 正则表达式，匹配只包含数字和字母的字符串
            string pattern = @"^[a-zA-Z0-9]+$";

            bool isMatch = Regex.IsMatch(input.UserName, pattern);
            if (!isMatch)
            {
                throw new UserFriendlyException("用户名不能包含除【字母】与【数字】的其他字符");
            }

            //校验手机号
            string mobileReg = @"^1[3-9]\d{9}$";
            bool isMobileNumber = Regex.IsMatch(input.Phone, mobileReg);
            if (!isMobileNumber)
            {
                throw new UserFriendlyException(UserConst.Phone_Irregular);
            }
        }
    }
}
